package com.ht.baselibs.retrofit.interceptor;


import com.alibaba.fastjson.JSON;
import com.ht.baselibs.utils.LogUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public abstract class PreParamsInterceptor implements Interceptor {

    private HttpUrl.Builder mUrlBuilder;
    private Request mOriginalRequest;

    @Override
    public Response intercept(Chain chain) throws IOException {

        mOriginalRequest = chain.request();
        if ("POST".equals(mOriginalRequest.method())){
            mUrlBuilder = mOriginalRequest.url().newBuilder();

            Map<String, String> paramsMap = getPostGlobalParams();
            Map<String, Object> oldParamsMap = new HashMap<>();

            RequestBody body = mOriginalRequest.body();
            FormBody normalBody = (FormBody) body;
            String serviceCode = "";
            for (int i = 0; i < normalBody.size(); i++) {
                String paramName = normalBody.name(i);
                String value = normalBody.value(i);

                if (!"serviceCode".equals(paramName)){
                    oldParamsMap.put(paramName, value);
                } else {
                    serviceCode = value;
                    paramsMap.put(paramName, value);
                }
            }

            if (!"HSY001016".equals(serviceCode)){
                String requestData = JSON.toJSONString(oldParamsMap, false);
                LogUtils.error(requestData);

                if (oldParamsMap.size() > 0){
                    paramsMap.put("requestData", requestData);
                }
            } else {
                paramsMap.put("requestData", oldParamsMap.get("requestData").toString());
            }

            FormBody.Builder newBody = new FormBody.Builder();
            for (Map.Entry<String, String> entry: paramsMap.entrySet()) {
                String name = entry.getKey();
                String value = entry.getValue();
                newBody.add(name, value);
            }

            Request newRequest = mOriginalRequest.newBuilder()
                    .url(mUrlBuilder.build())
                    .method(mOriginalRequest.method(), newBody.build())
                    .build();
            return chain.proceed(newRequest);
        } else {
            return chain.proceed(mOriginalRequest);
        }
    }


    protected abstract Map<String, String> getPostGlobalParams();

    protected Map<String, String> getParamNames(){
        Map<String, String> map = new HashMap<>();
        if (mOriginalRequest.body() instanceof FormBody){
            FormBody body = (FormBody) mOriginalRequest.body();
            if (body != null){
                for (int i = 0; i < body.size(); i++) {
                    String paramName = body.encodedName(i);
                    String paramValue = body.encodedValue(i);
                    map.put(paramName, paramValue);
                }
            }
        }

        return map;
    }

    protected void addGetParam(String key, String value){
        mUrlBuilder.addQueryParameter(key, value);
    }
}